#include "login.h"
#include "ui_login.h"
#include <QDebug>
#include <QPainter>

Login::Login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Login)
  , m_avatarPixmap(":/image/default_avatar.png")
{
    ui->setupUi(this);
    setAttribute(Qt::WA_StyledBackground, true);

    QPixmap pixmap(":/image/close_window.png");
    ui->close_window->setIcon(pixmap);
}

Login::~Login()
{
    delete ui;
}



void Login::paintEvent(QPaintEvent *event)
{
    (void)event;

    // 绘制圆形头像
    {
     // 直接在Widget上绘制
        QPainter painter(this);
        int x = 89, y = 39, w = 62, h = 62;
        QPainterPath path;
        path.addEllipse(x, y, w, h);
        painter.setClipPath(path);

        if (!m_avatarPixmap.isNull())
        {
            painter.drawPixmap(x + 1, y + 1, w - 2, h - 2, m_avatarPixmap);
        } else
        {
            // 处理加载失败的情况
            painter.fillRect(x + 1, y + 1, w - 2, h - 2, Qt::lightGray);
            painter.setClipPath(QPainterPath()); // 取消裁剪
            painter.drawText(x + 1, y + 1, w - 2, h - 2, Qt::AlignCenter, "!");
            return;
        }

        painter.setRenderHint(QPainter::Antialiasing); // 启用抗锯齿


        QPen pen; // 2像素宽的边框
        pen.setColor(Qt::black);
        pen.setWidth(2);
        painter.setPen(pen);
        painter.drawEllipse(x, y, w, h);
    }
}

void Login::on_close_window_clicked()
{
    emit closeWindow();
}

void Login::on_login_clicked()
{
    if(!ui->lineEdit_1->text().isEmpty() && !ui->lineEdit_2->text().isEmpty())
    {
        emit login(ui->lineEdit_1->text(), ui->lineEdit_2->text());
    }
    else
    {
        // 可以设计一个提示，提示用户需要输入账号和密码
    }
}

void Login::on_pushButton_clicked()
{
    emit userRegister();
}
